//
//  XS_MineTopCell.swift
//  XueDao
//
//  Created by fandongtongxue on 2020/12/8.
//

import UIKit

protocol XS_MineTopCellDelegate {
    
    func topCell(_ : XS_MineTopCell, didClickInfoBtn: UIButton)
    
    func topCell(_ : XS_MineTopCell, didClickNotificationBtn: UIButton)
    
    func topCell(_ : XS_MineTopCell, didClickVipBtn: UIImageView)
    
    func topCell(_ : XS_MineTopCell, didClickFavoriteBtn: QMUIButton)
    func topCell(_ : XS_MineTopCell, didClickApplyBtn: QMUIButton)
    func topCell(_ : XS_MineTopCell, didClickCertBtn: QMUIButton)
    func topCell(_ : XS_MineTopCell, didClickOrderBtn: QMUIButton)
    
    func topCell(_ : XS_MineTopCell, didClickIntegralBtn: QMUIButton)
    func topCell(_ : XS_MineTopCell, didClickCouponBtn: QMUIButton)
}

class XS_MineTopCell: UITableViewCell {
    
    var delegate : XS_MineTopCellDelegate?

    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var authImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var roleLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var vipImageView: UIImageView!
    
    @IBOutlet weak var favoriteBtn: QMUIButton!
    @IBOutlet weak var applyBtn: QMUIButton!
    @IBOutlet weak var certBtn: QMUIButton!
    @IBOutlet weak var orderBtn: QMUIButton!
    
    @IBOutlet weak var couponBtn: QMUIButton!
    @IBOutlet weak var integralBtn: QMUIButton!
    @IBOutlet weak var redPointView: UIView!
    
    
    @IBOutlet weak var openPreLabel: UILabel!
    @IBOutlet weak var openMidLabel: UILabel!
    @IBOutlet weak var openSufLabel: UILabel!
    
    @IBOutlet weak var vipLabel: UILabel!
    @IBOutlet weak var vipTimeLabel: UILabel!
    
    @IBOutlet weak var operateBtn: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        iconImageView.layer.cornerRadius = 27
        iconImageView.layer.borderWidth = 1
        iconImageView.layer.borderColor = UIColor.white.cgColor
        
        iconImageView.layer.shadowRadius = 7
        iconImageView.layer.shadowOffset = CGSize(width: 0, height: 4)
        iconImageView.layer.shadowColor = UIColor(red: 0.23, green: 0.50, blue: 0.49, alpha: 0.22).cgColor
        
        let vipTap = UITapGestureRecognizer.init(target: self, action: #selector(vipTapAction))
        vipImageView.addGestureRecognizer(vipTap)
        
        favoriteBtn.imagePosition = .top
        favoriteBtn.spacingBetweenImageAndTitle = 7
        applyBtn.imagePosition = .top
        applyBtn.spacingBetweenImageAndTitle = 7
        certBtn.imagePosition = .top
        certBtn.spacingBetweenImageAndTitle = 7
        orderBtn.imagePosition = .top
        orderBtn.spacingBetweenImageAndTitle = 7
        
//        couponBtn.imagePosition = .right
//        couponBtn.spacingBetweenImageAndTitle = 4
//        integralBtn.imagePosition = .right
//        integralBtn.spacingBetweenImageAndTitle = 4
        
//        couponBtn.imageEdgeInsets = UIEdgeInsets(top: 4, left: 0, bottom: 0, right: 0)
//        integralBtn.imageEdgeInsets = UIEdgeInsets(top: 4, left: 0, bottom: 0, right: 0)
    }
    
    func setButtonMaskView(numText: String, strText: String, imgName: String) -> UIView {
        let bgView = UIView()
        bgView.isUserInteractionEnabled = false
        let midLab = UILabel()
        midLab.font = .systemFont(ofSize: 13, weight: .semibold)
        midLab.textColor = UIColor(red: 0.27, green: 0.27, blue: 0.27, alpha: 1.00)
        midLab.textAlignment = .center
        midLab.text = strText
        bgView.addSubview(midLab)
        midLab.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
        }
        
        let startLab = UILabel()
        startLab.font = .systemFont(ofSize: 22, weight: .semibold)
        startLab.textColor = UIColor(red: 0.27, green: 0.27, blue: 0.27, alpha: 1.00)
        startLab.textAlignment = .right
        startLab.text = numText
        bgView.addSubview(startLab)
        startLab.snp.makeConstraints { (make) in
            make.centerY.equalTo(midLab)
            make.top.bottom.equalToSuperview()
            make.right.equalTo(midLab.snp.left)
            make.left.lessThanOrEqualToSuperview()
        }
        
        let endIv = UIImageView.init(image: UIImage.init(named: imgName))
        endIv.contentMode = .scaleAspectFit
        bgView.addSubview(endIv)
        endIv.snp.makeConstraints { (make) in
            make.centerY.equalTo(midLab)
            make.left.equalTo(midLab.snp.right).offset(4)
            make.width.height.equalTo(14)
            make.right.lessThanOrEqualToSuperview()
        }
        
        return bgView
    }
    
    func setBtnValues(couponStr: String, integralStr: String) {
        
        let couponBtnMaskV = setButtonMaskView(numText: "\(couponStr)", strText: " 优惠券", imgName: "编组 16")
        couponBtnMaskV.tag = 121314
        for item in couponBtn.subviews {
            if item.tag == 121314 {
                item.removeFromSuperview()
            }
        }
        couponBtn.addSubview(couponBtnMaskV)
        couponBtnMaskV.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
        
        let integralBtnMaskV = setButtonMaskView(numText: "\(integralStr)", strText: " 学到币", imgName: "编组 16_1")
        integralBtnMaskV.tag = 121315
        for item in integralBtn.subviews {
            if item.tag == 121315 {
                item.removeFromSuperview()
            }
        }
        integralBtn.addSubview(integralBtnMaskV)
        integralBtnMaskV.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
    }
    
    var _model : XS_UserBOM!
    public var model : XS_UserBOM!{
        set{
            _model = newValue
            //设置数据
            iconImageView.sd_setImage(with: URL.init(string: newValue.userImgSrc), completed: nil)
            nameLabel.text = newValue.username
            
            authImageView.isHidden = newValue.userTypeEnum() == .user
            
            if authImageView.isHidden {
                //普通用户
                openPreLabel.isHidden = false
                openMidLabel.isHidden = false
                openSufLabel.isHidden = false
                vipLabel.isHidden = true
                vipTimeLabel.isHidden = true
                operateBtn.setTitle("开通会员", for: .normal)
                operateBtn.snp.updateConstraints { (make) in
                    make.width.equalTo(70)
                }
            }else{
                //VIP
                vipLabel.isHidden = false
                vipTimeLabel.isHidden = false
                vipTimeLabel.text = newValue.vipExpirationTime+"到期"
                openPreLabel.isHidden = true
                openMidLabel.isHidden = true
                openSufLabel.isHidden = true
                operateBtn.setTitle("查看会员权益", for: .normal)
                operateBtn.snp.updateConstraints { (make) in
                    make.width.equalTo(92)
                }
            }
            
//            newValue.score
//            let scoreAttr = NSMutableAttributedString.init(string: newValue.money + " 学到币")
//            scoreAttr.addAttributes([.font :UIFont.systemFont(ofSize: 22, weight: .bold)], range: NSRange.init(location: 0, length: newValue.money.count))
//            integralBtn.setAttributedTitle(scoreAttr, for: .normal)
//
////            couponsNumber
//            let couponAttr = NSMutableAttributedString.init(string: newValue.couponsNumber + " 优惠券")
//            couponAttr.addAttributes([.font :UIFont.systemFont(ofSize: 22, weight: .bold)], range: NSRange.init(location: 0, length: newValue.couponsNumber.count))
//            couponBtn.setAttributedTitle(couponAttr, for: .normal)
            
            setBtnValues(couponStr: "\(newValue.couponsNumber)", integralStr: "\(newValue.money)")
            
            contentLabel.text = "学习时长：" + newValue.learningTime + "h"
            
            redPointView.isHidden = newValue.unreadCount == 0
            
        }
        get{
            return _model
        }
    }
    
    @objc func vipTapAction(){
        if delegate != nil {
            delegate?.topCell(self, didClickVipBtn: vipImageView)
        }
    }

    @IBAction func infoBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickInfoBtn: sender)
        }
    }
    
    @IBAction func couponBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickCouponBtn: sender)
        }
    }
    
    @IBAction func favoriteBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickFavoriteBtn: sender)
        }
    }
    
    @IBAction func applyBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickApplyBtn: sender)
        }
    }
    
    @IBAction func certBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickCertBtn: sender)
        }
    }
    
    @IBAction func orderBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickOrderBtn: sender)
        }
    }
    
    @IBAction func integralBtnAction(_ sender: QMUIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickIntegralBtn: sender)
        }
    }
    
    @IBAction func notificationBtnAction(_ sender: UIButton) {
        if delegate != nil {
            delegate?.topCell(self, didClickNotificationBtn: sender)
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
